package hips.gui.parameters;

import hips.gui.ComponentLocation;
import hips.images.Image;
import hips.images.ImagePartitionable;
import hips.pixel.PixelValue;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.EventListenerList;

public class ParameterFrame extends javax.swing.JFrame {
    Image img;
    PixelValue defaultAlpha;
    PixelValue defaultOmega;
    float defaultCindex;
    ParameterPanel[] slice;
    ParameterPanel allSlices;

    protected EventListenerList listenerList;

    String tabTitle;

    public ParameterFrame(ImagePartitionable img) {
        initComponents();

        defaultCindex = 0f;
        defaultAlpha = img.getMaxRange();
        defaultOmega = img.getMaxRange();
        defaultAlpha.div(3);
        defaultOmega.div(2);

        ComponentLocation.setLocationCenter(0.25f, 0.5f, this);

        listenerList = new EventListenerList();

        cindex.setFont(new java.awt.Font("Calibri", 0, 16));
        jButton1.setFont(new java.awt.Font("Calibri", 0, 16));
        jButton2.setFont(new java.awt.Font("Calibri", 0, 16));
        jButton3.setFont(new java.awt.Font("Calibri", 0, 16));
        jLabel3.setFont(new java.awt.Font("Calibri", 0, 16));
        jRadioButton1.setFont(new java.awt.Font("Calibri", 0, 16));
        jTabbedPane1.setFont(new java.awt.Font("Calibri", 0, 16));


        this.img = img;


        Settings settings = null;
        if (img instanceof hips.images.gray8.ImageGray8)
            settings = new hips.gui.parameters.integer.Settings();
        else
            settings = new hips.gui.parameters.floating.Settings();

        cindex.setModel(new SpinnerNumberModel(
            defaultCindex,
            0.0f,
            1.0f,
            settings.STEP_CI
        ));

        if (img.getSlices() <= 6){
            tabTitle = "slice ";
        }
        else{
            tabTitle = "";
        }

        slice = new ParameterPanel[img.getSlices()];
        for (int i = 0; i < img.getSlices(); i++){
            slice[i] = new ParameterPanel(
                (Number)defaultAlpha.getValue(i),
                (Number)defaultOmega.getValue(i),
                img.getMaxRange().getValue(i),
                img.getMaxValue().range(img.getMinValue()).getValue(i)
            );
        }

        if (img.getSlices() > 1){
            allSlices = new ParameterPanel(
                (Number)defaultAlpha.minValue(),
                (Number)defaultOmega.minValue(),
                img.getMaxRange().minValue(),
                img.getMaxValue().range(img.getMinValue()).minValue()
            );
            JSpinner[] alpha = new JSpinner[img.getSlices()];
            JSpinner[] omega = new JSpinner[img.getSlices()];
            for (int i = 0; i < img.getSlices(); i++){
                alpha[i] = slice[i].getAlphaSpinner();
                omega[i] = slice[i].getOmegaSpinner();
            }
            allSlices.getAlphaSpinner().addChangeListener(new ChangeParameterListener(alpha));
            allSlices.getOmegaSpinner().addChangeListener(new ChangeParameterListener(omega));
        }
        else{
            jRadioButton1.setEnabled(false);
        }

        setDefaults();
        setVisible(true);
    }

    public void addParameterListener(ParameterListener listener) {
        listenerList.add(ParameterListener.class, listener);
    }

    public void removeParameterListener(ParameterListener listener) {
        listenerList.remove(ParameterListener.class, listener);
    }


    private void setDefaults(){
        cindex.setValue((double)defaultCindex);
        if (img.getSlices() > 1){
            allSlices.setAlpha(defaultAlpha.getValue(0));
            allSlices.setOmega(defaultOmega.getValue(0));
        }
        for (int i = 0; i < img.getSlices(); i++){
            slice[i].setAlpha(defaultAlpha.getValue(i));
            slice[i].setOmega(defaultOmega.getValue(i));
        }
        if (img.getSlices() > 1 && defaultAlpha.maxValue() == defaultAlpha.minValue() && defaultOmega.maxValue() == defaultOmega.minValue()){
            jRadioButton1.setSelected(false);
            tabOne();
        }
        else{
            jRadioButton1.setSelected(true);
            tabAll();
        }
    }

    private void tabAll(){
        jTabbedPane1.removeAll();
        for (int i = 0; i < img.getSlices(); i++){
            jTabbedPane1.insertTab(tabTitle + i, null, slice[i], null, i);
        }
    }

    private void tabOne(){
        jTabbedPane1.removeAll();
        jTabbedPane1.insertTab("all slices", null, allSlices, null, 0);
    }

    public PixelValue getAlpha(){
        PixelValue p = img.newPixelValue();
        for (int i = 0; i < img.getSlices(); i++){
            p.setValue(slice[i].getAlpha(), i);
        }
        return p;
    }

    public PixelValue getOmega(){
        PixelValue p = img.newPixelValue();
        for (int i = 0; i < img.getSlices(); i++){
            p.setValue(slice[i].getOmega(), i);
        }
        return p;
    }

    public float getCindex(){
        return ((Double)cindex.getValue()).floatValue();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jRadioButton1 = new javax.swing.JRadioButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jLabel3 = new javax.swing.JLabel();
        cindex = new javax.swing.JSpinner();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

        jRadioButton1.setText("edit individual slices");
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        jButton1.setText("ok");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("set default values");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jTabbedPane1.setBackground(new java.awt.Color(246, 246, 246));

        jLabel3.setText("Connectivity index:");

        jButton3.setText("cancel");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(cindex, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jButton1)
                            .addGap(18, 18, 18)
                            .addComponent(jButton3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton2))
                        .addComponent(jRadioButton1)
                        .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 413, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(cindex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 48, Short.MAX_VALUE)
                .addComponent(jRadioButton1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1)
                    .addComponent(jButton3))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        setDefaults();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Thread t = new Thread(new Runnable(){
            public void run(){
                Object[] listeners = listenerList.getListenerList();
                for (int i = 0; i < listeners.length; i += 2) {
                    if (listeners[i] == ParameterListener.class) {
                        ((ParameterListener) listeners[i + 1]).ParametersOk(getAlpha(), getOmega(), getCindex());
                    }
                }
            }
        });
        t.start();
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
        if (jRadioButton1.isSelected()){
            tabAll();
        }
        else{
            tabOne();
        }
    }//GEN-LAST:event_jRadioButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        Thread t = new Thread(new Runnable(){
            public void run(){
                Object[] listeners = listenerList.getListenerList();
                for (int i = 0; i < listeners.length; i += 2) {
                    if (listeners[i] == ParameterListener.class) {
                        ((ParameterListener) listeners[i + 1]).ParametersCancelled();
                    }
                }
            }
        });
        t.start();
        dispose();
    }//GEN-LAST:event_jButton3ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSpinner cindex;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables

}
